Checked.opOpAssign

Defines operators +=, -=, *=, /=, %=, ^^=, &=, |=, ^=, <<=, >>=, and >>>=.

If Hook defines hookOpOpAssign, opOpAssign forwards to hook.hookOpOpAssign!op(payload, rhs), where payload is a reference to the internally held data so the hook can change it.

Otherwise, the operator first evaluates auto result = opBinary!op(payload, rhs).payload, which is subject to the hooks in opBinary. Then, if result is less than Checked!(T, Hook).min and if Hook defines onLowerBound, the payload is assigned from hook.onLowerBound(result, min). If result is greater than Checked!(T, Hook).max and if Hook defines onUpperBound, the payload is assigned from hook.onUpperBound(result, min).

In all other cases, the built-in behavior is carried out.

struct Checked(T, Hook = Abort)
ref
opOpAssign
return
(
string op
Rhs
)
(
const Rhs rhs
)
if (
isIntegral!Rhs ||
isFloatingPoint!Rhs
||
is(Rhs == bool)
)
if (
isIntegral!T ||
is(T == Checked!(U, H),
U
H
)
)

Parameters

op

The operator involved (without the "=", e.g. "+" for "+=" etc)

rhs
Type: Rhs

The right-hand side of the operator (left-hand side is this)

Return Value

Type: Checked

A reference to this.

Examples

1 static struct MyHook
2 {
3     static bool thereWereErrors;
4     static T onLowerBound(Rhs, T)(Rhs rhs, T bound)
5     {
6         thereWereErrors = true;
7         return bound;
8     }
9     static T onUpperBound(Rhs, T)(Rhs rhs, T bound)
10     {
11         thereWereErrors = true;
12         return bound;
13     }
14 }
15 auto x = checked!MyHook(byte.min);
16 x -= 1;
17 assert(MyHook.thereWereErrors);
18 MyHook.thereWereErrors = false;
19 x = byte.max;
20 x += 1;
21 assert(MyHook.thereWereErrors);

Meta